<template>
    <div class="EditPunishPopup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
				<el-form-item label="惩戒类别" prop="Category">
					<el-select class="w-[280px]" v-model="formData.Category">
                        <el-option v-for="(item, key) in optionData.PunishCategory" :key="key" :label="item.name"
                            :value="item.value" />
                    </el-select>
				</el-form-item>
				<el-form-item label="惩戒原因" prop="Reason">
                    <el-input v-model="formData.Reason" placeholder="请输入惩戒原因" clearable />
                </el-form-item>
				<el-form-item label="批准日期" prop="Date">
                    <el-date-picker v-model="formData.Date" type="date" />
                </el-form-item>
				<el-form-item label="解除惩戒时间" prop="RemoveDate">
                    <el-date-picker v-model="formData.RemoveDate" type="date" />
                </el-form-item>
				<el-form-item label="批准机关名称" prop="OrganizationName">
                    <el-input v-model="formData.OrganizationName" placeholder="请输入批准机关名称" clearable />
                </el-form-item>
				<el-form-item label="批准机关代码" prop="OrganizationCode">
                    <el-input v-model="formData.OrganizationCode" placeholder="请输入批准机关代码" clearable />
                </el-form-item>
				<el-form-item label="批准惩戒文号" prop="DocumentName">
                    <el-input v-model="formData.DocumentName" placeholder="请输入批准惩戒文号" clearable />
                </el-form-item>
				<el-form-item label="惩戒变化时间" prop="DateOfChange">
                    <el-date-picker v-model="formData.DateOfChange" type="date" />
                </el-form-item>
				<el-form-item label="惩戒变化类别" prop="CategoryOfChange">
                    <el-select class="w-[280px]" v-model="formData.CategoryOfChange">
                        <el-option v-for="(item, key) in optionData.CategoryOfChange" :key="key" :label="item.name"
                            :value="item.value" />
                    </el-select>
                </el-form-item>
				<el-form-item label="惩戒变化文号" prop="DocumentNameOfChange">
                    <el-input v-model="formData.DocumentNameOfChange" placeholder="请输入惩戒变化文号" clearable />
                </el-form-item>
				<el-form-item label="受惩戒时职务" prop="PunishingPostion">
				    <el-input v-model="formData.PunishingPostion" clearable />
				</el-form-item>
				<el-form-item label="撤销变更原因" prop="	RemoveReason">
				    <el-input v-model="formData.RemoveReason" clearable />
				</el-form-item>
				<el-form-item label="惩戒说明" prop="Remark">
				    <el-input v-model="formData.Remark" clearable />
				</el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup name="EditPunishPopup">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { useDictData } from '@/hooks/useDictOptions'
import { EditPunish } from '@/api/consumer'
const { dictData: optionData } = useDictData('PunishCategory,CategoryOfChange')
console.log('惩戒类别',optionData)
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑学历信息' : '添加学历信息'
})
const props = defineProps(['member_id']) /**从view.vue传递参数 */
const formData = reactive({
    id: 0,
    member_id: props.member_id,
     Category: '',
     Reason: '',
     Date: '',
     RemoveDate: '',
     OrganizationName: '',
     OrganizationCode: '',
     DocumentName: '',
	 DateOfChange: '',
	 CategoryOfChange: '',
	 DocumentNameOfChange: '',
	 PunishingPostion: '',
	 RemoveReason: '',
	 Remark: '',
})

const formRules = reactive({
     Category: [
         {
             required: true,
             message: '请输入惩戒类别',
             trigger: ['blur']
         }
     ],
     Reason: [
         {
             required: true,
             message: '请输入惩戒原因',
             trigger: ['blur']
         }
     ],
     Date: [
         {
             required: true,
             message: '请输入批准日期',
             trigger: ['blur']
         }
     ],
     OrganizationName: [
         {
             required: true,
             message: '请选择批准机关名称',
             trigger: ['blur']
         }
     ],
     DocumentName: [
         {
             required: true,
             message: '请选择批准惩戒文号',
             trigger: ['blur']
         }
     ]
})


const handleSubmit = async () => {
    await formRef.value?.validate()
    console.log('学历表单内容', formData)
    await EditPunish(formData)
    //mode.value == 'edit' ? await adminEdit(formData) : await adminAdd(formData)
    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (row: any) => {
    formData.id = row.id
    const data = await EditPunish({
        id: row.id,
    		 mode:'get'
    })
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            formData[key] = data[key]
        }
    }
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData
})
</script>
